#ifndef CNPIPE_JSONHANDLER_H
#define CNPIPE_JSONHANDLER_H

#include "cnpipe_global.h"
#include "cnpipe_piperangedefiner.h"
#include "cnpipe_singleelement.h"
#include "cnpipe_singlenode.h"

#include <QDebug>
#include <QFileDialog>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonParseError>
#include <QJsonValue>
#include <QList>
#include <QObject>


/**
 * 读取管道展示专用JSON文件管理器，包括了管道展示所用的节点与单元
 * @brief The CNPIPE_jsonHandler class
 */
class CNPIPE_jsonHandler : public QObject {
    Q_OBJECT
public:
    CNPIPE_jsonHandler (CNPIPE_global *par) {
        this->parent = par;
        this->nodesArray = new QList<CNPIPE_singleNode*>();
        this->elementsArray = new QList<CNPIPE_singleElement*>();
    }
    void openJSONFile() {
        //QMessageBox::information(this, tr("Information"), tr("Open"));
        QFileDialog *fd = new QFileDialog();
    //    QString fileName = fd->getOpenFileName(NULL,"打开JSON格式的文件　",
    //                        "D:\\CNPE programming\\samples",
    //                        "*.json");

        fd->setDirectory("D:\\CNPE programming\\samples");
        fd->setNameFilter("*.json");

        if (fd->exec() == QDialog::Accepted) {
            qDebug() << tr("我就系公平正义　");
            QList<QString> nameList = fd->selectedFiles();
            int count = nameList.length();
            for (int i = 0; i < count; i++) {
                QFile data(nameList.at(i));
                if (data.open(QIODevice::ReadOnly | QIODevice::Text)) {
                    QString value = data.readAll();
                    qDebug() << value;
                    data.close();

                    QJsonParseError parseJsonErr;
                    QJsonDocument document = QJsonDocument::fromJson(value.toUtf8(), &parseJsonErr);
                    if (!(parseJsonErr.error == QJsonParseError::NoError)) {
                        qDebug() << tr("Error parsing single JSON file.");
                        return;
                    }
                    QJsonObject jsonObject = document.object();
                    QJsonObject data1 = jsonObject.value(QStringLiteral("data")).toObject();

                    nodes = data1.value("nodes").toArray();
                    geometry = data1.value(QStringLiteral("geometry")).toArray();

                    frequencyDisplacements = data1.value(QStringLiteral("frequencyDisplacements")).toObject();
                    materials = data1.value(QStringLiteral("materials")).toObject();
                    pipeConditions = data1.value(QStringLiteral("pipeConditions")).toObject();
                    spectrumInfo = data1.value(QStringLiteral("spectrumInfo")).toObject();

                    qDebug() << "nodes: " << nodes.count();
                    qDebug() << "geometry: " << geometry.count();
                    //qDebug() << "frequency displacements: " << frequencyDisplacements.keys();

                    //清除所有的节点信息
                    this->clearAllNodes();
                    for (int n = 0; n < nodes.count(); n++) {
                        QJsonObject nodeJSON = nodes.at(n).toObject();
                        double x = nodeJSON.value(QStringLiteral("x")).toDouble();
                        double y = nodeJSON.value(QStringLiteral("y")).toDouble();
                        double z = nodeJSON.value(QStringLiteral("z")).toDouble();
                        QString node = nodeJSON.value(QStringLiteral("node")).toString();
                        QString type = nodeJSON.value(QStringLiteral("type")).toString();
                        QString constraintName = nodeJSON.value(QStringLiteral("constraintName")).toString();
                        QString constraint = nodeJSON.value(QStringLiteral("constraints")).toString();

                        CNPIPE_singleNode *nodeObject
                                = new CNPIPE_singleNode(x, y, z, node, type, constraintName, constraint);
                        this->nodesArray->push_back(nodeObject);
                        //qDebug() << node << " " << type << " " << constraintName << " " << constraint;
                    }

                    //清除所有的单元信息
                    this->clearAllElements();
                    for (int e = 0; e < geometry.count(); e++) {
                        QJsonObject elementJSON = geometry.at(e).toObject();

                        QString element = elementJSON.value(QStringLiteral("element")).toString();
                        QString node1 = elementJSON.value(QStringLiteral("node1")).toString();
                        QString node2 = elementJSON.value(QStringLiteral("node2")).toString();
                        QString type = elementJSON.value(QStringLiteral("type")).toString();
                        QString mat = elementJSON.value(QStringLiteral("mat")).toString();
                        double angle = elementJSON.value(QStringLiteral("angle")).toDouble();

                        CNPIPE_singleElement *singleElement
                                = new CNPIPE_singleElement(element, node1, node2, type, mat, angle);

                        elementsArray->push_back(singleElement);
                        qDebug() << elementJSON;
                    }

                    CNPIPE_pipeRangeDefiner *pipeRangeDefiner = (CNPIPE_pipeRangeDefiner *)parent->getPipeRangeDefiner();
                    pipeRangeDefiner->initializePipeForDisplaying(nodesArray, elementsArray);

                    qDebug() << "new nodes: " << this->nodesArray->length();
                }

            }
        }
        //qDebug() << fileName;
    }
    void clearAllNodes() {
        this->nodesArray->clear();
    }
    void clearAllElements() {
        this->elementsArray->clear();
    }
private:
    CNPIPE_global *parent;

    QJsonArray nodes;
    QJsonArray geometry;

    QJsonObject frequencyDisplacements;
    QJsonObject materials;
    QJsonObject pipeConditions;
    QJsonObject spectrumInfo;

    QList<CNPIPE_singleNode*> *nodesArray;
    QList<CNPIPE_singleElement*> *elementsArray;
};

#endif // CNPIPE_JSONHANDLER_H
